package com.hejjon.knowledgebase.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hejjon.knowledgebase.constant.MessageConstant;
import com.hejjon.knowledgebase.constant.event.EventKeys;
import com.hejjon.knowledgebase.entity.EntryProperty;
import com.hejjon.knowledgebase.exception.BusinessException;
import com.hejjon.knowledgebase.mapper.EntryPropertyMapper;
import com.hejjon.knowledgebase.model.request.EntryPropertyRequest;
import com.hejjon.knowledgebase.model.wrapper.DataGrid;
import com.hejjon.knowledgebase.service.EntryPropertyService;
import com.hejjon.knowledgebase.util.BeanMapUtil;
import com.hejjon.knowledgebase.util.EventParamsHolder;
import com.hejjon.knowledgebase.util.Valid;
import com.hejjon.knowledgebase.vo.EntryPropertyVO;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author:
 * @date: 2022-12-08 14:16:27
 * @since: 1.0
 */
@Service
public class EntryPropertyServiceImpl implements EntryPropertyService {

    @Resource
    private EntryPropertyMapper entryPropertyMapper;


    @Override
    public Long addEntryProperty(EntryPropertyRequest request) {

        EntryProperty insert = new EntryProperty();
        insert.setName(request.getName());
        insert.setClassroom(request.getClassroom());
        insert.setCount(request.getCount() == null ? 1 : request.getCount());
        insert.setRepairEndTime(request.getRepairEndTime());
        insert.setRepairFactory(request.getRepairFactory());
        insert.setEntryTime(LocalDateTime.now());
        insert.setEntryUser(request.getEntryUser());
        insert.setBuyTime(LocalDateTime.now());
        insert.setCreateTime(LocalDateTime.now());

        entryPropertyMapper.insert(insert);

        // 设置日志参数
        EventParamsHolder.addParams(BeanMapUtil.bean2Map(insert));
        EventParamsHolder.addParam(EventKeys.NAME, insert.getName());
        return insert.getId();
    }


    @Override
    public Integer deleteEntryProperty(List<Long> idList) {
        int effect = 0;
        for (Long id : idList) {
            effect += entryPropertyMapper.deleteById(id);
        }

        // 设置日志参数
        EventParamsHolder.addParam(EventKeys.ID_LIST, idList);
        EventParamsHolder.addParam(EventKeys.COUNT, effect);
        return effect;
    }

    @Override
    public Integer updateEntryProperty(EntryPropertyRequest request) {

        Long id = request.getId();
        EntryProperty entryProperty = entryPropertyMapper.selectById(id);
        Valid.notNull(entryProperty, MessageConstant.UNKNOWN_DATA);

        EntryProperty update = new EntryProperty();
        update.setId(id);
        update.setClassroom(request.getClassroom());
        update.setCount(request.getCount() == null ? 1 : request.getCount());
        update.setName(request.getName());
        update.setRepairEndTime(request.getRepairEndTime());
        update.setRepairFactory(request.getRepairFactory());
        update.setEntryUser(request.getEntryUser());
        update.setBuyTime(request.getBuyTime());
        update.setUpdateTime(LocalDateTime.now());
        int effect = entryPropertyMapper.updateById(update);

        // 日志参数
        EventParamsHolder.addParams(BeanMapUtil.bean2Map(update));
        EventParamsHolder.addParam(EventKeys.NAME, update.getName());
        return effect;
    }

    @Override
    public DataGrid<EntryPropertyVO> listEntryProperty(EntryPropertyRequest request) {

        Page<EntryProperty> page = new Page<>(request.getPage(), request.getLimit());

        LambdaQueryWrapper<EntryProperty> wrapper = new LambdaQueryWrapper<EntryProperty>()
                .like(StringUtils.isNotBlank(request.getName()), EntryProperty::getName, request.getName())
                .like(StringUtils.isNotBlank(request.getClassroom()), EntryProperty::getClassroom, request.getClassroom())
                .like(StringUtils.isNotBlank(request.getEntryUser()), EntryProperty::getEntryUser, request.getEntryUser())
                .orderByDesc(EntryProperty::getCreateTime);

        Page<EntryProperty> entryPropertyPage = entryPropertyMapper.selectPage(page, wrapper);
        Page<EntryPropertyVO> voPage = new Page<>();
        voPage.setSize(entryPropertyPage.getSize());
        voPage.setCurrent(entryPropertyPage.getCurrent());
        voPage.setPages(entryPropertyPage.getPages());
        voPage.setTotal(entryPropertyPage.getTotal());
        List<EntryProperty> records = entryPropertyPage.getRecords();
        List<EntryPropertyVO> voRecords = new ArrayList<>();
        try {
            for (EntryProperty entryProperty : records) {
                EntryPropertyVO vo = new EntryPropertyVO();
                BeanUtils.copyProperties(vo, entryProperty);
                voRecords.add(vo);
            }
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new BusinessException(MessageConstant.OPERATOR_ERROR);
        }
        voPage.setRecords(voRecords);
        return DataGrid.of(voPage);
    }

    @Override
    public EntryPropertyVO getEntryProperty(Long id) {
        EntryProperty entryProperty = entryPropertyMapper.selectById(id);
        if (entryProperty == null) {
            throw new BusinessException(MessageConstant.UNKNOWN_DATA);
        }

        EntryPropertyVO vo = new EntryPropertyVO();
        try {
            BeanUtils.copyProperties(vo, entryProperty);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new BusinessException(MessageConstant.OPERATOR_ERROR);
        }

        return vo;
    }
}
